package com.cjt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjt.common.dto.StuListClaInfo;
import com.cjt.common.vo.StuListScoreVo;
import com.cjt.pojo.*;
import com.cjt.service.*;
import com.cjt.mapper.ScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Hello
* @description 针对表【score】的数据库操作Service实现
* @createDate 2023-03-06 22:29:30
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
    implements ScoreService{
    @Autowired
    Stu_ScoreService stu_scoreService;
    @Autowired
    Sub_ScoreService subScoreService;
    @Autowired
    SubjetService subjetService;
    @Autowired
    TeacherService teacherService;


    @Override
    public List<StuListScoreVo> getScore(StuListClaInfo info) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        StuListScoreVo stuListScoreVo = new StuListScoreVo();
        List<StuListScoreVo> list = new ArrayList<>();
        //拿到学号
        String id = authentication.getName();
        //通过学号到 学生成绩中间表 拿到 成绩id
        List<Stu_Score> stu_score = stu_scoreService.list(new QueryWrapper<Stu_Score>().eq("stu_id", id));
        for (Stu_Score stuScore : stu_score) {
            Long scoreId = stuScore.getScoreId();
            //通过成绩id在 成绩科目表 中拿到科目id
            Sub_Score score_id = subScoreService.getOne(new QueryWrapper<Sub_Score>().eq("score_id", scoreId));
            Long subId = score_id.getSubId();

            //通过科目id在 科目表 中拿到科目名和老师id
            Subjet subjet = subjetService.getOne(new LambdaQueryWrapper<Subjet>()
                    .eq(Subjet::getId,subId)
                    .like(info.getInput()!=null,Subjet::getName,info.getInput()));
            if (!ObjectUtil.isEmpty(subjet)) {
                stuListScoreVo.setName(subjet.getName());
                Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq("tea_id", subjet.getTeaId()));
                stuListScoreVo.setTeaName(teacher.getName());

                //通过成绩id在 成绩表 中找到对应的分数和状态码
                Score score = this.getOne(new LambdaQueryWrapper<Score>()
                        .eq(Score::getId, scoreId)
                        .eq(info.getRadio()!=null,Score::getStatu, info.getRadio()));

                if (!ObjectUtil.isEmpty(score)){
                    stuListScoreVo.setScore(score.getScore());
                    stuListScoreVo.setStatus(score.getStatu());
                    stuListScoreVo.setCreated(score.getCreated());
                    stuListScoreVo.setUpdated(score.getUpdated());
                    StuListScoreVo news = BeanUtil.copyProperties(stuListScoreVo, StuListScoreVo.class);
                    list.add(news);
                }
            }


        }





        return list;
    }
}




